package com.brillilab.starter.controller.aio.experiments;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.domain.enums.experiment.TimerOperationEnum;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.experiments.ExperimentStepTimerLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 实验计时器
 * @author WuMenghao
 * @time   2019/04/29
 */
@RestController
@RequestMapping("/api/experiment/step/timer")
public class ExperimentStepTimerController {

    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ExperimentStepTimerLogic experimentStepTimerLogic;

    @AspectLog
    @RequestMapping(value="/operate",method=RequestMethod.GET)
    public ResponseVo timerOperation(Long experimentId, Long phaseId, Long stepId, Integer operation, @RequestHeader(value=StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId!=null && phaseId!=null && stepId!=null && operation!=null && StringUtils.isNotBlank(token)," 请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        if(operation.equals(TimerOperationEnum.PAUSE.getOperation())){
            //暂停
            experimentStepTimerLogic.pauseStepTimers(experimentId,phaseId,stepId,userInfo.getLabId(),userInfo.getLabMemberId());
        }else if(operation.equals(TimerOperationEnum.START.getOperation())){
            //开始
            experimentStepTimerLogic.startStepTimers(experimentId,phaseId,stepId,userInfo.getLabId(),userInfo.getLabMemberId());
        }else if(operation.equals(TimerOperationEnum.RESET.getOperation())){
            //重置
            experimentStepTimerLogic.resetStepTimers(experimentId,phaseId,stepId,userInfo.getLabId(),userInfo.getLabMemberId());
        }else {
            throw new BrillilabException("操作: "+operation+" 不存在！");
        }

        return ResponseVo.success();
    }

    /**
     * 实验步骤中计时器删除
     * @param experimentId
     * @param phaseId
     * @param stepId
     * @param timerId
     * @param token
     * @return
     */
    @AspectLog
    @RequestMapping(value="/delete",method= RequestMethod.DELETE)
    public ResponseVo timerDelete(Long experimentId, Long phaseId, Long stepId, Long timerId, @RequestHeader(value= StarterConstant.TOKEN)String token){

        Assert.isTrue(experimentId != null && phaseId != null && stepId != null && timerId != null && StringUtils.isNotBlank(token), "请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        experimentStepTimerLogic.timerDelete(experimentId,phaseId,stepId,timerId,userInfo);

        return ResponseVo.success();
    }
}
